package com.kt.thread.demo;

import java.util.concurrent.*;

/**
 * Created by Administrator on 2015/5/9 0009.
 */
public class Program {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    int j = 0;
    private int i = 0;


    /*public void test() {
        for (; i < 300; i++) {
            System.out.println("子线程:" + i + ";thread_name" + Thread.currentThread().getName());
        }
    }*/

    public static void main(String[] args) throws Exception {
        //com.kt.thread.demo.ConditionTest3.test();
        //new com.kt.thread.demo.ConditionTest2().test();
        //com.kt.thread.demo.DeadLockTest.test();
        //com.kt.thread.demo.WaitAndNotifyTest.test();
        //com.kt.thread.demo.ConditionTest conditionTest = new com.kt.thread.demo.ConditionTest();
        //conditionTest.testPuterAndTake();
        //com.kt.thread.demo.ThreadLocalTest threadLocalTest=com.kt.thread.demo.ThreadLocalTest.getInstance();
        // Demo2.main();
        //com.kt.thread.demo.ConditionTest conditionTest = new com.kt.thread.demo.ConditionTest();
        //new com.kt.thread.demo.SemaphoreTest().test();
        //conditionTest.testPuterAndTake();
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (com.kt.thread.demo.Program.class) {
                    while (i < 150) {
                        test();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (com.kt.thread.demo.Program.class) {
                    while (i < 150) {
                        System.out.println("子线程2:" + i++);
                    }
                    System.out.println(i);
                }
            }
        }).start();*/

        //conditionTest.conditionTest();
       /* ExecutorService executorService= Executors.newSingleThreadExecutor();
        com.kt.thread.demo.CompletionServiceTest test=new com.kt.thread.demo.CompletionServiceTest();
        //test.poll(executorService);
        test.take(executorService);
        final com.kt.thread.demo.Exercises com.kt.thread.demo.exercises=new com.kt.thread.demo.Exercises();*/
        //com.kt.thread.demo.ThreadPool threadPool=new com.kt.thread.demo.ThreadPool();
        //threadPool.testCachedThreadPool();
        //threadPool.testSingleThreadPool();
        /*AtomicInteger atomicInteger=new AtomicInteger();
        AtomicIntegerFieldUpdater<com.kt.thread.demo.Exercises> fieldUpdater=AtomicIntegerFieldUpdater.newUpdater(com.kt.thread.demo.Exercises.class, "i");*/
        //int result=fieldUpdater.addAndGet(com.kt.thread.demo.exercises,5);
        //System.out.println(result);
        //int i=atomicInteger.addAndGet(1);
        /*System.out.println(atomicInteger.addAndGet(0));
        System.out.println(atomicInteger.get());
        System.out.println(atomicInteger.addAndGet(-1));
        System.out.println(atomicInteger.get());*/

       /* for(int i=0;i<2;i++){
            com.kt.thread.demo.exercises.run();
        }*/
        /*
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=1;i<=3;i++){
                    com.kt.thread.demo.exercises.sub(i);
                }
            }
        }).start();
        for (int i=1;i<=3;i++){
            com.kt.thread.demo.exercises.main(i);
        }*/

//        final com.kt.thread.demo.SynchronizedTest t1=new com.kt.thread.demo.SynchronizedTest();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true){
//                    t1.output3("ABC");
//                    try {
//                        Thread.sleep(10);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }).start();
//
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true){
//                    t1.output4("XYZ");
//                    try {
//                        Thread.sleep(10);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }).start();
//        new Timer().schedule(new com.kt.thread.demo.MyTimerTask(),2000);


        //开始测试读写锁,如果出现某个线程正在读取数据，还没读取完成，另一个线程就开始写入数据，或者
        //某个线程正在写入数据，还没写入完成，另一个线程就开始读取数据，说明是不正确的程序
        //final com.kt.thread.demo.ReadWriteLockTest readWriteLockTest = new com.kt.thread.demo.ReadWriteLockTest();
        //开3个读锁，读锁可以并发读取可能打印出两个读取线程同时去读取数据,
        //但不会有两个写入线程同时写入数据
       /* for (int i = 0; i < 3; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            readWriteLockTest.readData();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
            //开3个写锁，只有一个写锁完成后，其他锁才能进去
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        readWriteLockTest.writeData();
                    }
                }
            }).start();
        }*/

         CountDownLatchTest.test();

        //测试实现callable接口的多线程
       /* FutureTask<Integer> task = new FutureTask<Integer>(new com.kt.thread.demo.CallableTest());
        new Thread(task).start();
        //获取返回值
        try {
            System.out.println("callable接口的返回值:" + task.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }*/

        //测试Suspend方法引起的死锁
        //com.kt.thread.demo.TestSuspendThread.test();
        //测试线程池
        //com.kt.thread.demo.ThreadPool.test();

        //测试volatile
        /*com.kt.thread.demo.VolatileTest vt=new com.kt.thread.demo.VolatileTest();
        vt.test();
        while (Thread.activeCount()>2){
            System.out.println("activeCount:"+Thread.activeCount());
            Thread.yield();
        }
        System.out.println("i:"+vt.getInc());*/


        /*
         final com.kt.thread.demo.Program p = new com.kt.thread.demo.Program();
        Thread[]threads=new Thread[10];
        for(int i=0;i<10;i++){
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    p.test();
                }
            },"A"+i);
        }

        for(int i=0;i<10;i++){
            threads[i].start();
        }*/
        System.out.println("执行完成");

        /*Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    p.test2();
                }
            }, "A" + i);
        }

        for (int i = 0; i < 10; i++) {
            threads[i].start();
        }*/

    }

    /*public void test2() {

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                //System.out.println(i++);
                test();
            }
        });
    }*/
}

